package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.search.repository.IFlightRepository;
import cn.wolfcode.luowowo.serach.query.FlightTemplateQueryObject;
import cn.wolfcode.luowowo.serach.service.IFlightSeivice;
import cn.wolfcode.luowowo.serach.template.FlightTemplate;
import cn.wolfcode.luowowo.serach.ulit.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class FlightSeiviceImpl implements IFlightSeivice {

    @Autowired
    private IFlightRepository repository;

    @Autowired
    private ElasticsearchTemplate template;

    @Override
    public List<FlightTemplate> query(FlightTemplateQueryObject qo) {

        AssertUtil.hasLength(qo.getFromCityCode(),"起点不可为空");
        AssertUtil.hasLength(qo.getToCityCode(),"目的地不可为空");

        Date takeoffTime = qo.getTakeoffTime();
        Date stratDate = null;
        Date endDate = null;
        if (takeoffTime != null) {
            stratDate = DateUtil.getStratDate(takeoffTime);
            endDate = DateUtil.getEndDate(takeoffTime);
        }else {
            stratDate = DateUtil.getStratDate(new Date());
            endDate = DateUtil.getEndDate(stratDate);
        }

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();

        BoolQueryBuilder query = QueryBuilders.boolQuery();
        query.must(QueryBuilders.matchQuery("fromCityCode", qo.getFromCityCode()))
                .must(QueryBuilders.matchQuery("toCityCode", qo.getToCityCode()))
                .filter(QueryBuilders.rangeQuery("takeoffTime").gte(stratDate.getTime()).lte(endDate.getTime()));


        if (StringUtils.hasLength(qo.getTakeoffPortName())) {
            query.must(QueryBuilders.matchQuery("takeoffPortName", qo.getTakeoffPortName()));
        }

        if (StringUtils.hasLength(qo.getLandingPortName())) {
            query.must(QueryBuilders.matchQuery("landingPortName", qo.getLandingPortName()));
        }

        if (StringUtils.hasLength(qo.getAirlineName())) {
            query.must(QueryBuilders.matchQuery("airlineName", qo.getAirlineName()));
        }

        if (StringUtils.hasLength(qo.getPlaneType())) {
            query.must(QueryBuilders.matchQuery("planeType", qo.getPlaneType()));
        }


        builder.withQuery(query);

        List<FlightTemplate> list = template.queryForList(builder.build(), FlightTemplate.class);

        return list;
    }

    @Override
    public void saveOrUpdate(FlightTemplate flightTemplate) {
        repository.save(flightTemplate);
    }

    @Override
    public FlightTemplate findById(Long ftId) {
        Optional<FlightTemplate> byId = repository.findById(ftId);
        return byId.get();
    }
}
